<?php

namespace app\api\controller;

use app\payment\PayFactory;
use app\services\AdService;
use app\services\ConfigService;
use app\services\PropertyService;
use plugin\admin\app\model\AdNotify;
use plugin\admin\app\model\UserPropertyLog;
use support\Db;
use support\Request;

class AdController
{
    public function handle(Request $request)
    {
        $data = $request->post();

        try {
            Db::beginTransaction();

            self::checkSign($data['jihuoniao_tradeNumber']);

            AdNotify::insert([
                'trade_number' => $data['jihuoniao_tradeNumber'],
                'ads_id' => $data['jihuoniao_adsId'],
                'user_id' => $data['jihuoniao_userId'],
                'extra_info' => $data['jihuoniao_extraInfo'],
                'created_time' => date('Y-m-d H:i:s')
            ]);

            Db::commit();

            AdService::reward($data);

            return 'success';
        } catch (\Exception $e) {
            Db::rollBack();

        }
    }

    public static function checkSign($tradeNumber)
    {
        $secret = ConfigService::get('callback_secret');
        return hash('sha256', $tradeNumber . $secret);
    }
}